<template>
  <div class="tags-view">
    <el-scrollbar class="tags-view-wrapper">
      <router-link
        v-for="tag in visitedViews"
        :key="tag.path"
        :class="isActive(tag) ? 'active' : ''"
        class="tags-view-item"
        :to="{ path: tag.path, query: tag.query }"
      >
        <el-icon v-if="tag.meta?.icon">
          <component :is="tag.meta.icon" />
        </el-icon>
        {{ tag.meta?.title }}
        <el-icon 
          class="close-icon"
          v-if="visitedViews.length > 1"
          @click.prevent.stop="closeSelectedTag(tag)"
        >
          <Close />
        </el-icon>
      </router-link>
    </el-scrollbar>
    <div class="tags-view-actions">
      <el-dropdown trigger="click" @command="handleCommand">
        <el-button size="small" type="primary" plain>
          标签操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="closeOthers">关闭其他</el-dropdown-item>
            <el-dropdown-item command="closeAll">关闭所有</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Close, ArrowDown } from '@element-plus/icons-vue'
import type { RouteLocationNormalized } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 访问过的路由标签
const visitedViews = ref<RouteLocationNormalized[]>([])

// 添加标签
const addView = (view: RouteLocationNormalized) => {
  if (view.meta?.noTagsView) return
  const isExists = visitedViews.value.some(v => v.path === view.path)
  if (!isExists) {
    visitedViews.value.push(Object.assign({}, view))
  }
}

// 关闭标签
const closeSelectedTag = (view: RouteLocationNormalized) => {
  const index = visitedViews.value.findIndex(v => v.path === view.path)
  if (index > -1) {
    visitedViews.value.splice(index, 1)
    if (isActive(view)) {
      toLastView()
    }
  }
}

// 关闭其他标签
const closeOthersTags = () => {
  visitedViews.value = visitedViews.value.filter(v => {
    return v.meta?.affix || v.path === route.path
  })
}

// 关闭所有标签
const closeAllTags = () => {
  visitedViews.value = visitedViews.value.filter(tag => tag.meta?.affix)
  router.push('/')
}

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'closeOthers':
      closeOthersTags()
      break
    case 'closeAll':
      closeAllTags()
      break
  }
}

// 跳转到最后一个标签
const toLastView = () => {
  const latestView = visitedViews.value.slice(-1)[0]
  if (latestView) {
    router.push(latestView.path)
  } else {
    router.push('/')
  }
}

// 判断是否是激活标签
const isActive = (view: RouteLocationNormalized) => {
  return view.path === route.path
}

// 监听路由变化
router.afterEach((to) => {
  if (to.meta?.title) {
    addView(to)
  }
})

// 初始化
if (route.meta?.title) {
  addView(route)
}
</script>

<style scoped lang="scss">
.tags-view {
  height: 34px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  display: flex;
  
  .tags-view-wrapper {
    flex: 1;
    padding: 0 16px;
    .tags-view-item {
      display: inline-flex;
      align-items: center;
      height: 26px;
      line-height: 26px;
      padding: 0 8px;
      font-size: 12px;
      margin: 4px 4px 0;
      color: #495060;
      background: #fff;
      border: 1px solid #d8dce5;
      border-radius: 3px;
      cursor: pointer;
      text-decoration: none;
      transition: all .3s cubic-bezier(.645, .045, .355, 1);
      
      .el-icon {
        margin-right: 4px;
        width: 14px;
        height: 14px;
      }
      
      .close-icon {
        width: 14px;
        height: 14px;
        margin-left: 4px;
        border-radius: 50%;
        text-align: center;
        transition: all .3s cubic-bezier(.645, .045, .355, 1);
        transform-origin: 100% 50%;
        
        &:hover {
          background-color: #b4bccc;
          color: #fff;
        }
      }
      
      &:hover {
        color: #409EFF;
        border-color: #c6e2ff;
        background-color: #ecf5ff;
      }
      
      &.active {
        color: #fff;
        border-color: #409EFF;
        background-color: #409EFF;
        
        &::before {
          content: '';
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 4px;
        }
        
        .close-icon {
          &:hover {
            background-color: #fff;
            color: #409EFF;
          }
        }
      }
    }
  }
  
  .tags-view-actions {
    padding: 0 16px;
    display: flex;
    align-items: center;
  }
}
</style> 